package com.qianfeng.user.controller;

import java.io.IOException;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qianfeng.utils.ImgCode;


@WebServlet("/user/getImgCode")
public class GetImgCode extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//生成随机数字
		Random random = new Random();
		String code = "";
		int num = random.nextInt(10);
		code += num;
		
		num = random.nextInt(10);
		code += num;
		
		num = random.nextInt(10);
		code += num;
		
		num = random.nextInt(10);
		code += num;
		
		//设置图片的宽和高,输出流和验证码
		ImgCode.outputImage(100, 30, resp.getOutputStream(), code);
		
		//String code = ImgCode.outputVerifyImage(500, 100, resp.getOutputStream(), 4);
		
		req.getSession().setAttribute("imgCode", code);
		
	}

}
